package com.brillilab.service.core.topics.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brillilab.dao.mapper.topics.TopicTargetMapper;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.topics.TopicDirection;
import com.brillilab.domain.po.topics.TopicTarget;
import com.brillilab.domain.vo.lab.MemberInfoVo;
import com.brillilab.domain.vo.topics.TopicTargetAddVo;
import com.brillilab.service.core.experiments.IExperimentService;
import com.brillilab.service.core.plans.IPlanService;
import com.brillilab.service.core.plans.IPlanTargetGroupService;
import com.brillilab.service.core.topics.ITopicDirectionService;
import com.brillilab.service.core.topics.ITopicExperimentService;
import com.brillilab.service.core.topics.ITopicTargetService;

import cn.hutool.core.collection.CollUtil;

@Service
@Transactional
public class TopicTargetServiceImpl implements ITopicTargetService {

	@Resource
	private TopicTargetMapper topicTargetMapper;
	@Resource
	private ITopicDirectionService topicDirectionService;
	@Resource
	private ITopicExperimentService topicExperimentService;
	@Resource
	private IPlanService planService;
	@Resource
	private IPlanTargetGroupService planTargetGroupService;
	@Resource
	private IExperimentService experimentService;

	@Override
	public List<TopicTarget> getListByTopicId(List<Long> topicIds) {
		return topicTargetMapper.selectList(new LambdaQueryWrapper<TopicTarget>().in(TopicTarget::getTopicId, topicIds)
				.eq(TopicTarget::getIsDelete, BoolEnum.FALSE.getValue()));
	}

	@Override
	public TopicTarget targetAdd(TopicTargetAddVo addVo) {
		TopicTarget target = new TopicTarget();
		target.setName(addVo.getName());
		target.setLabId(addVo.getLabId());
		TopicDirection direction = topicDirectionService.getById(addVo.getTopicDirectionId());
		target.setTopicId(direction.getTopicId());
		target.setTopicDirectionId(direction.getId());
		int count = this.getCount(direction.getId(), null);
		target.setFigure("实验流" + direction.getFigure().replaceAll("研究内容", "") + "." + (count + 1));
		
		int totalCount = this.getCountByTopicId(direction.getTopicId(), null);
		List<String> colorList = CollUtil.toList("#EB5444", "#F6AC2D", "#00CB7C","#70AACE", "#797BDA", "#B763A1", "#BAA753");
		int i = totalCount % colorList.size();
		target.setColor(colorList.get(i));
		int b = topicTargetMapper.insert(target);
		Assert.isTrue(b == 1, "添加失败");
		return target;
	}

	@Override
	public int getCount(Long directionId, Integer isDelete) {
		LambdaQueryWrapper<TopicTarget> queryWrapper = new LambdaQueryWrapper<TopicTarget>();
		queryWrapper.eq(TopicTarget::getTopicDirectionId, directionId);
		if (isDelete != null)
			queryWrapper.eq(TopicTarget::getIsDelete, isDelete);
		return topicTargetMapper.selectCount(queryWrapper);
	}
	
	@Override
	public int getCountByTopicId(Long topicId, Integer isDelete) {
		LambdaQueryWrapper<TopicTarget> queryWrapper = new LambdaQueryWrapper<TopicTarget>();
		queryWrapper.eq(TopicTarget::getTopicId, topicId);
		if (isDelete != null)
			queryWrapper.eq(TopicTarget::getIsDelete, isDelete);
		return topicTargetMapper.selectCount(queryWrapper);
	}

	@Override
	public void updateById(TopicTarget target) {
		int b = topicTargetMapper.updateById(target);
		Assert.isTrue(b == 1, "更新失败");
	}

	@Override
	public void delete(TopicTarget target) {
		Long topicTargetId = target.getId();
		TopicTarget lastOne = this.getLastOne(target.getTopicDirectionId());
		if (lastOne != null && !lastOne.getId().equals(topicTargetId)) {
			target.setIsDelete(BoolEnum.TRUE.getValue());
			int b = topicTargetMapper.updateById(target);
			Assert.isTrue(b == 1, "删除失败");
		} else {
			int b = topicTargetMapper.deleteById(lastOne);
			Assert.isTrue(b == 1, "删除失败");
		}
		topicExperimentService.delete(null, topicTargetId);
		planTargetGroupService.delete(target.getTopicId(), target.getTopicDirectionId(), topicTargetId);
		planService.deleteByTopicTargetId(topicTargetId);
		experimentService.delete(target.getTopicDirectionId(), topicTargetId, null);
	}

	@SuppressWarnings("unchecked")
	private TopicTarget getLastOne(Long topicDirectionId) {
		LambdaQueryWrapper<TopicTarget> queryWrapper = new LambdaQueryWrapper<TopicTarget>();
		queryWrapper.eq(TopicTarget::getTopicDirectionId, topicDirectionId);
		queryWrapper.orderByDesc(TopicTarget::getId);
		List<TopicTarget> list = topicTargetMapper.selectList(queryWrapper);
		return CollectionUtils.isNotEmpty(list) ? list.get(0) : null;
	}

	@Override
	public void deleteByDirectionId(Long topicDirectionId) {
		TopicTarget target = new TopicTarget();
		target.setIsDelete(BoolEnum.TRUE.getValue());
		;
		topicTargetMapper.update(target,
				new LambdaQueryWrapper<TopicTarget>().eq(TopicTarget::getTopicDirectionId, topicDirectionId));
	}

	@Override
	public List<TopicTarget> getTargetList(Long topicDirectionId) {
		return topicTargetMapper
				.selectList(new LambdaQueryWrapper<TopicTarget>().eq(TopicTarget::getTopicDirectionId, topicDirectionId)
						.eq(TopicTarget::getIsDelete, BoolEnum.FALSE.getValue()));
	}

	@Override
	public List<TopicTarget> getTargetList(Long topicDirectionId, int size) {
		IPage<TopicTarget> page = new Page<TopicTarget>(1, size);
		LambdaQueryWrapper<TopicTarget> queryWrapper = new LambdaQueryWrapper<TopicTarget>()
				.eq(TopicTarget::getTopicDirectionId, topicDirectionId)
				.eq(TopicTarget::getIsDelete, BoolEnum.FALSE.getValue());
		return topicTargetMapper.selectPage(page, queryWrapper).getRecords();
	}

	@Override
	public List<TopicTarget> selectListByTopicId(Long topicId) {
		return topicTargetMapper.selectListByTopicId(topicId);
	}

	@Override
	public TopicTarget selectById(Long topicTargetId) {
		return topicTargetMapper.selectById(topicTargetId);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<TopicTarget> selectListByTopicDirectionId(Long topicDirectionId) {
		return topicTargetMapper
				.selectList(new LambdaQueryWrapper<TopicTarget>().eq(TopicTarget::getTopicDirectionId, topicDirectionId)
						.eq(TopicTarget::getIsDelete, BoolEnum.FALSE.getValue()).orderByAsc(TopicTarget::getFigure));
	}

	@Override
	public List<MemberInfoVo> getTargetMemberList(Long targetId) {
		return topicTargetMapper.getTargetMemberList(targetId);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<TopicTarget> selectByIds(List<Long> topicTargetIds) {
		return topicTargetMapper.selectList(new LambdaQueryWrapper<TopicTarget>().in(TopicTarget::getId, topicTargetIds)
				.eq(TopicTarget::getIsDelete, BoolEnum.FALSE.getValue()).orderByAsc(TopicTarget::getId));
	}

	@Override
	public List<TopicTarget> getTargetAllList() {
		return topicTargetMapper.selectList(null);
	}

}